#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package Robo::Config;
use strict;
use warnings;
use XML::Simple;

# Constructor
sub new
{
        my ($class, $main, $mod) = @_;
        
        my $self = { };
        
        $self->{main_file} = $main;
        $self->{main} = undef;
        $self->{mod_file} = $mod;
        $self->{mod} = undef;
        $self->{nick} = undef;
        
        load_config($self, $main, $mod);
        
        bless($self);
        return $self;
}

# Current nickname.  (Probably shouldn't be in here, I will deal with
# it later.)
sub nick
{
        my ($self, $nick) = @_;
        
        if ($nick) { $self->{nick} = $nick; }
        else {
                if ($self->{nick}) {
                        return $self->{nick};
                }
                return $self->get_cfg("bot/nick");
        }
}

# Load configuration.
sub load_config
{
        my ($self) = @_;
        
        $self->{main} = XMLin($self->{main_file}) or die "Error loading ".$self->{main_file}."]\n";
        $self->{mod} = XMLin($self->{mod_file}) or die "Error loading ".$self->{mod_file}."]\n";
}

# Get main config.
sub get_cfg
{
        my ($self, $key) = @_;
        
        return $self->get($self->{main}, $key);
}

# Set config option in the module config.
sub set_mod
{
        my ($self, $key, $value) = @_;
        
        my @sec = split(/\//, $key);
        my $h = $self->{mod};
        
        # Go through each section.
        foreach my $i (0..$#sec - 1) {
                # Section exists.
                if (defined($h->{$sec[$i]})) {
                        # Make sure this is a HASH, if not, this won't work.
                        if (ref($h->{$sec[$i]}) ne 'HASH') { return 0; }
                        
                        # Set current hashref.
                        $h = $h->{$sec[$i]};
                # Section does not exist, create it.
                } else {
                        # Create section...
                        $h->{$sec[$i]} = { };
                        
                        # ... and set current hashref.
                        $h = $h->{$sec[$i]};
                }
        }
        
        # Set to value.
        $h->{$sec[$#sec]} = $value;
        
        # Write to disk.
        $self->write_mod();
}

# Delete module config option.
sub del_mod
{
        my ($self, $key) = @_;
        
        my @sec = split(/\//, $key);
        my $h = $self->{mod};
        
        # Go through each section.
        foreach my $i (0..$#sec - 1) {
                # Check that this section exists.
                if (!defined($h->{$sec[$i]}) || ref($h->{$sec[$i]}) ne 'HASH') {
                        return;
                }
                
                $h = $h->{$sec[$i]};
        }
        
        # Check that this key exists.
        if (!defined($h->{$sec[$#sec]})) { return; }
        
        # Delete the key/value.
        delete $h->{$sec[$#sec]};
        
        # Write to disk.
        $self->write_mod();
}

# Write current module configuration to disk.
sub write_mod
{
        my $self = shift;
        
        # Write to disk.
        open my $fh, '>', $self->{mod_file};
        XMLout($self->{mod}, OutputFile => $fh);
        close $fh;
}

# Get mod config.
sub get_mod
{
        my ($self, $key) = @_;
        
        return $self->get($self->{mod}, $key);
}

# Get config option from the module config.
sub get
{
        my ($self, $hashref, $key) = @_;
        
        my @sec = split(/\//, $key);
        my $h = $hashref;
        
        # Go through each section.
        foreach my $i (0..$#sec - 1) {
                # Check that this section exists.
                if (!defined($h->{$sec[$i]}) || ref($h->{$sec[$i]}) ne 'HASH') {
                        return undef;
                }
                
                $h = $h->{$sec[$i]};
        }
        
        # Check that this key exists.
        if (!defined($h->{$sec[$#sec]})) { return undef; }
        
        # Return value.
        return $h->{$sec[$#sec]};
}

1;
